<?php namespace App\Models\Power;

use App\Models\Base\PowerBase;
use App\Models\Power\Pile;

class User extends PowerBase {

    protected $table = 'cdz_user';

    protected $fillable = [];

    protected $dates = [];

    public static $rules = [
        // Validation rules
    ];
    public $timestamps = false;


    /**
     * 根据uid查询用户信息
     * params1 String $uid
     */
    public function myMoney($uid) {

        $query = self::query();
        $query -> where('uid', '=', $uid);
        $data = $query -> first();

        if($data) {
            (array)$data;
        }

        return $data;
        
    }

    /**
     * 增加用户积分
     * @param int $userId
     * @param int $integral
     * @return bool
     */
    public static function addIntegral($userId = 0, $integral = 0)
    {
        $query = self::query();
        $query -> where('id', $userId);
        $user = $query -> first();
        if ($user) {
            $user->integral = (string)round($user->integral + $integral, 2);
            return $user->save();
        }
        return false;
    }

    /**
     * 增加用户余额
     * @param int $userId
     * @param int $integral
     * @return bool
     */
    public static function addMoney($userId = 0, $money = 0)
    {
        $query = self::query();
        $query -> where('id', $userId);
        $user = $query -> first();
        if ($user) {
            $user->money = (string)round($user->money + $money, 2);
            return $user->save();
        }
        return false;
    }

    /**
     * 返回直属上级用户
     * @param $nowUser
     * @param bool $real
     * @return null|static
     */
    public static function getSuperior($nowUser, $real = false)
    {

        $query = self::query();
        $query -> where('id', $nowUser->superior);
        $query -> whereIn('status', [1,2]);
        $user = $query -> first();
        if ($real) {
            $position = $nowUser->position;
            while (true) {
                if (!$user || $user->position != $position) {
                    break;
                }
                    $query = self::query();
                    $query -> where('id', $nowUser->superior);
                    $query -> whereIn('status', [1,2]);
                    $user = $query -> first();
            }
        }
        return $user;
    }

    /**
     * 返回以前上级用户
     * @param $nowUser
     * @return null|static
     */
    public static function getLastSuperior($nowUser)
    {
        $query = self::query();
        $query -> where('id', $nowUser->superior);
        $query -> whereIn('status', [1,2]);
        $user = $query -> first();
        return $user;
    }

    /**
     * 获取销售分成用户
     * @param $nowUser
     * @return array
     */
    public static function getSellIncomeUser($nowUser)
    {
        if (!$nowUser) {
            return [];
        }
        $income[8] = $nowUser->id;
        $superior = self::getSuperior($nowUser);
        if ($superior) {
            $income = self::getIncomeType();
            self::arrSet($income);
            switch ($superior->position) {
                case 1;
                    $income[7] = $superior->id;
                    break;
                case 2;
                    $income[1] = $superior->id;
                    break;
                case 3;
                    $income[2] = $superior->id;
                    $superior = self::getSuperior($superior);
                    if (!$superior) {
                        break;
                    }
                    if ($superior->position == 2) {
                        $income[1] = $superior->id;
                    }
                    break;
                case 4;
                    $income[3] = $superior->id;
                    $superior = self::getSuperior($superior);
                    if (!$superior) {
                        break;
                    }
                    switch ($superior->position) {
                        case 2;
                            $income[1] = $superior->id;
                            break;
                        case 3;
                            $income[2] = $superior->id;
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            if ($superior->position == 2) {
                                $income[1] = $superior->id;
                            }
                            break;
                    }
                    break;
                case 5;
                    $income[7] = $superior->id;
                    $income[4] = $superior->id;
                    $lastSuperior = self::getLastSuperior($superior);
                    if ($lastSuperior && $lastSuperior->position == 5) {
                        $income[5] = $lastSuperior->id;
                    }
                    $superior = self::getSuperior($superior);
                    if (!$superior) {
                        break;
                    }
                    switch ($superior->position) {
                        case 2;
                            $income[1] = $superior->id;
                            break;
                        case 3;
                            $income[2] = $superior->id;
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            if ($superior->position == 2) {
                                $income[1] = $superior->id;
                            }
                            break;
                        case 4;
                            $income[3] = $superior->id;
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            switch ($superior->position) {
                                case 2;
                                    $income[1] = $superior->id;
                                    break;
                                case 3;
                                    $income[2] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    if ($superior->position == 2) {
                                        $income[1] = $superior->id;
                                    }
                                    break;
                            }
                            break;
                    }
                    break;
                case 6;
                    $income[7] = $superior->id;
                    $superior = self::getSuperior($superior);
                    if (!$superior) {
                        break;
                    }
                    switch ($superior->position) {
                        case 2;
                            $income[1] = $superior->id;
                            break;
                        case 3;
                            $income[2] = $superior->id;
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            if ($superior->position == 2) {
                                $income[1] = $superior->id;
                            }
                            break;
                        case 4;
                            $income[3] = $superior->id;
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            switch ($superior->position) {
                                case 2;
                                    $income[1] = $superior->id;
                                    break;
                                case 3;
                                    $income[2] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    if ($superior->position == 2) {
                                        $income[1] = $superior->id;
                                    }
                                    break;
                            }
                            break;
                        case 5;
                            $income[6] = $superior->id;
                            $income[4] = $superior->id;
                            $lastSuperior = self::getLastSuperior($superior);
                            if ($lastSuperior && $lastSuperior->position == 5) {
                                $income[5] = $lastSuperior->id;
                            }
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            switch ($superior->position) {
                                case 2;
                                    $income[1] = $superior->id;
                                    break;
                                case 3;
                                    $income[2] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    if ($superior->position == 2) {
                                        $income[1] = $superior->id;
                                    }
                                    break;
                                case 4;
                                    $income[3] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    switch ($superior->position) {
                                        case 2;
                                            $income[1] = $superior->id;
                                            break;
                                        case 3;
                                            $income[2] = $superior->id;
                                            $superior = self::getSuperior($superior);
                                            if (!$superior) {
                                                break;
                                            }
                                            if ($superior->position == 2) {
                                                $income[1] = $superior->id;
                                            }
                                            break;
                                    }
                                    break;
                            }
                            break;
                    }
                    break;
                case 7;
                    $income[7] = $superior->id;
                    $superior = self::getSuperior($superior, true);
                    if (!$superior) {
                        break;
                    }
                    $father = self::getSuperior($nowUser);
                    if (!$father) {
                        break;
                    }
                    if ($father->superior == $superior->id) {
                        $income[6] = $superior->id;
                    }
                    switch ($superior->position) {
                        case 2;
                            $income[1] = $superior->id;
                            break;
                        case 3;
                            $income[2] = $superior->id;
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            if ($superior->position == 2) {
                                $income[1] = $superior->id;
                            }
                            break;
                        case 4;
                            $income[3] = $superior->id;
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            switch ($superior->position) {
                                case 2;
                                    $income[1] = $superior->id;
                                    break;
                                case 3;
                                    $income[2] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    if ($superior->position == 2) {
                                        $income[1] = $superior->id;
                                    }
                                    break;
                            }
                            break;
                        case 5;
                            $income[4] = $superior->id;
                            $lastSuperior = self::getLastSuperior($superior);
                            if ($lastSuperior && $lastSuperior->position == 5) {
                                $income[5] = $lastSuperior->id;
                            }
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            switch ($superior->position) {
                                case 2;
                                    $income[1] = $superior->id;
                                    break;
                                case 3;
                                    $income[2] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    if ($superior->position == 2) {
                                        $income[1] = $superior->id;
                                    }
                                    break;
                                case 4;
                                    $income[3] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    switch ($superior->position) {
                                        case 2;
                                            $income[1] = $superior->id;
                                            break;
                                        case 3;
                                            $income[2] = $superior->id;
                                            $superior = self::getSuperior($superior);
                                            if (!$superior) {
                                                break;
                                            }
                                            if ($superior->position == 2) {
                                                $income[1] = $superior->id;
                                            }
                                            break;
                                    }
                                    break;
                            }
                            break;
                        case 6;
                            $superior = self::getSuperior($superior);
                            if (!$superior) {
                                break;
                            }
                            switch ($superior->position) {
                                case 2;
                                    $income[1] = $superior->id;
                                    break;
                                case 3;
                                    $income[2] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    if ($superior->position == 2) {
                                        $income[1] = $superior->id;
                                    }
                                    break;
                                case 4;
                                    $income[3] = $superior->id;
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    switch ($superior->position) {
                                        case 2;
                                            $income[1] = $superior->id;
                                            break;
                                        case 3;
                                            $income[2] = $superior->id;
                                            $superior = self::getSuperior($superior);
                                            if (!$superior) {
                                                break;
                                            }
                                            if ($superior->position == 2) {
                                                $income[1] = $superior->id;
                                            }
                                            break;
                                    }
                                    break;
                                case 5;
                                    $income[4] = $superior->id;
                                    $lastSuperior = self::getLastSuperior($superior);
                                    if ($lastSuperior && $lastSuperior->position == 5) {
                                        $income[5] = $lastSuperior->id;
                                    }
                                    $superior = self::getSuperior($superior);
                                    if (!$superior) {
                                        break;
                                    }
                                    switch ($superior->position) {
                                        case 2;
                                            $income[1] = $superior->id;
                                            break;
                                        case 3;
                                            $income[2] = $superior->id;
                                            $superior = self::getSuperior($superior);
                                            if (!$superior) {
                                                break;
                                            }
                                            if ($superior->position == 2) {
                                                $income[1] = $superior->id;
                                            }
                                            break;
                                        case 4;
                                            $income[3] = $superior->id;
                                            $superior = self::getSuperior($superior);
                                            if (!$superior) {
                                                break;
                                            }
                                            switch ($superior->position) {
                                                case 2;
                                                    $income[1] = $superior->id;
                                                    break;
                                                case 3;
                                                    $income[2] = $superior->id;
                                                    $superior = self::getSuperior($superior);
                                                    if (!$superior) {
                                                        break;
                                                    }
                                                    if ($superior->position == 2) {
                                                        $income[1] = $superior->id;
                                                    }
                                                    break;
                                            }
                                            break;
                                    }
                                    break;
                            }
                            break;
                    }
                    break;
            }
        }
        return $income;
    }

    public function scopeWhereStatusNotIn($query) {

        return $query -> whereNotIn('status', [6,7]);

    }

    /**
     * 和User模型的多对一关联
     */
    // public function pile() {
    //     return $this->belongsTo('App\Models\Pile', 'pile_id');
    // }



}
